package com.kkh.tp;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.DatagramChannel;

import android.util.Log;

public class SCUdpChannel extends SCChannel {
	String tag="TpUdp";
	public SCUdpChannel(SCTransport tp) {
		super(tp, 1	);
		
	}
	
	public int write(byte[] buf, int len) {
		int wcnt;
		DatagramChannel sch = (DatagramChannel)socketChannel;
		try {
			wcnt = sch.write(ByteBuffer.wrap(buf, 0, len));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			wcnt = -100;
		}
		return wcnt;
	}
	
	public int read(byte[] buf) {
		return read(buf, null);
	}
	
	public int read(byte[] buf, InetSocketAddress remoteaddr) {
		int rdcnt;
		Log.d(tag, "udp read event....");
		DatagramChannel sch = (DatagramChannel)socketChannel;
		ByteBuffer bytebuffer = ByteBuffer.wrap(buf);
		try {
			remoteaddr = (InetSocketAddress) sch.receive(bytebuffer);
			
			rdcnt = bytebuffer.position();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			rdcnt = -100;
		}
		
		return rdcnt;
	}
}
